
package org.lq.ht.dao.impl;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.CashDao;
import org.lq.ht.entity.Cash;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;

/**
 * 现金接口实现
 * @author 郑奥宇
 * @phone 15993025053
 *
 * @package org.lq.ht.dao.impl
 * @date 2020年8月20日下午9:06:52
 */
public class CashDaoImpl implements CashDao,Instantiation<Cash>{

	/**
	 * 添加
	 */
	@Override
	public int save(Cash t) {
		String sql = "insert into cash(operator,return_money,out_money,note)values(?,?,?,?)";
		int num = 0;
		try {
			num = JDBCUtil.executeUpdate(sql, t.getOperator(),t.getReturn_money(),t.getOut_money(),t.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 修改
	 */
	@Override
	public int update(Cash t) {
		int num = 0;
		String sql = "update cash set operator=?,return_money=?,out_money=?,note=? where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getOperator(),t.getReturn_money(),t.getOut_money(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 删除
	 */
	@Override
	public int delete(int no) {
		int num = 0;
		String sql = "delete from cash where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
		
	}
	/**
	 * 查询全部
	 */
	@Override
	public List<Cash> findAll() {
		List<Cash> list = new ArrayList<>();
		String sql = "select * from cash";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 通过序号查询
	 */
	@Override
	public Cash getById(int no) {
		List<Cash> list = new ArrayList<>();
		String sql = "select * from cash where no = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);			
		}
		return null;
	}
	@Override
	public Cash instance(ResultSet rs) {
		Cash cash = new Cash();
		try {
			cash.setNo(rs.getInt("no"));
			cash.setOperator(rs.getString("operator"));
			cash.setReturn_money(rs.getDouble("return_money"));
			cash.setOut_money(rs.getDouble("out_money"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cash;
	}

}

